package com.yide.myworld.domain;

import cn.hutool.core.lang.UUID;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MQMessageDataDto implements Serializable {

    /**
     * 消息id
     */
    private String messageId;
    /**
     * 消息体（发送消息时需要手动序列化;消费者使用时再反序列化）
     */
    private String data;

    public MQMessageDataDto(Object data) {
        if (data instanceof JSONObject) {
            this.data = data.toString();
        } else if (data instanceof com.alibaba.fastjson.JSONObject) {
            this.data = data.toString();
        } else {
            this.data = JSON.toJSONString(data);
        }
        this.messageId = UUID.fastUUID().toString(true);
    }
}
